/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Communication;

import Request.GeneralRequest;
import java.io.IOException;
import java.net.ServerSocket;
import sun.java2d.loops.GeneralRenderer;
import java.util.*;

/**
 *
 * @author Zhang Ying
 */
public class MailThread extends Thread implements RequestHandler
{
    private ServerSocket MailSocket;


    public MailThread(int port) throws Exception
    {
        MailSocket = new ServerSocket(port);
        System.out.println("Mail server is started.");
    }

    //The interface
    public void handleGeneralRequest(GeneralRequest aRequest)
    {
        System.out.println(aRequest.getRequestType());
        if(aRequest.getRequestType() == GeneralRequest.GRType.Invitation)
        {
            System.out.println(aRequest.getFieldValue("From"));
            System.out.println(aRequest.getFieldValue("To"));
        }

    }

    public void run()
    {
        while(true)
        {
            try
            {
                PeerThread tempPeer = new PeerThread(MailSocket.accept(), this);
                System.out.println("New peer is connected.");
                tempPeer.start();
            }
            catch(IOException e)
            {
                System.out.println(e.getMessage());
            }

        }
    }
}
